
//
//  CommitServersViewController.m
//  RenRen
//
//  Created by 杜文杰 on 2018/7/20.
//

#import "CommitServersViewController.h"
#define kHEIGHT 150
@interface CommitServersViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
@property (nonatomic, strong) UITableView *mainTableView; // 主框架
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIView *footerView;
@property (nonatomic, strong) UIButton *chooseStypeNameBtn;      // 选择的分类显示
@end

@implementation CommitServersViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.mainTableView];
    self.title = @"发布消息";
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint point = scrollView.contentOffset;
    if (point.y < -kHEIGHT) {
        CGRect rect = [self.mainTableView viewWithTag:101].frame;
        rect.origin.y = point.y;
        rect.size.height = -point.y;
        [self.mainTableView viewWithTag:101].frame = rect;
    }
}

#pragma mark -UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01f;
}
#pragma mark - init
- (UITableView *)mainTableView
{
    if (!_mainTableView) {
        _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, SafeAreaTopHeight, SCREEN_WIDTH, SCREEN_HEIGHT - SafeAreaBottomHeight - SafeAreaTopHeight) style:UITableViewStyleGrouped];
        _mainTableView.backgroundColor = getColor(whiteColor);
        _mainTableView.tableHeaderView = self.headerView;
        _mainTableView.tableFooterView = self.footerView;
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        _mainTableView.showsVerticalScrollIndicator = NO;
        _mainTableView.showsHorizontalScrollIndicator = NO;
        _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        // 动态行高
        _mainTableView.rowHeight = UITableViewAutomaticDimension;
        // 预估行高
        _mainTableView.estimatedRowHeight = 80;
        // 设置UITableview的内容偏移来为图片视图留出位置
        _mainTableView.contentInset = UIEdgeInsetsMake(kHEIGHT, 0, 0, 0);
        [_mainTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -kHEIGHT, SCREEN_WIDTH, kHEIGHT)];
        
        imageView.image = [UIImage imageNamed:@"fruit-default@2x"];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.tag = 101;
        [_mainTableView addSubview:imageView];
        // 拍照
        UIImageView *pzimgView = [[UIImageView alloc]initWithFrame:CGRectZero];
        pzimgView.image = [UIImage imageNamed:@""];
        pzimgView.backgroundColor = [UIColor redColor];
        [_mainTableView addSubview:pzimgView];
        [pzimgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(imageView);
            make.centerY.equalTo(imageView);
            make.width.height.mas_equalTo(100);
        }];
        
    }
    return _mainTableView;
}
- (UIView *)headerView
{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
//        _headerView.backgroundColor = [UIColor grayColor];
        
        _chooseStypeNameBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 20 / HEIGHT_6S_SCALE, SCREEN_WIDTH, 40 / HEIGHT_6S_SCALE)];
        _chooseStypeNameBtn.backgroundColor = getColor(bgColor);
        [_chooseStypeNameBtn setTitleColor:getColor(blackColor) forState:UIControlStateNormal];
        _chooseStypeNameBtn.titleLabel.font = DEF_FontSize_14;
//        [_chooseStypeNameBtn setImage:[UIImage imageNamed:@"list_wjleftimg"] forState:UIControlStateNormal];
        [_chooseStypeNameBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -10 / WIDTH_6S_SCALE, 0, 0)];
//        [_chooseStypeNameBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 50 / WIDTH_6S_SCALE, 0,0 / WIDTH_6S_SCALE)];
        [_chooseStypeNameBtn setTitle:@"你选择的分类是:装修建材 --> 装修设计 --> 全包装修" forState:UIControlStateNormal];
        [_headerView addSubview:_chooseStypeNameBtn];
        
//        CGRectMake(10 / WIDTH_6S_SCALE, CGRectGetMaxY(_chooseStypeNameBtn.frame), SCREEN_WIDTH - 20 / WIDTH_6S_SCALE, 80 / HEIGHT_6S_SCALE)
        UIView *titleView = [[UIView alloc]initWithFrame:CGRectZero];
        titleView.backgroundColor = [UIColor clearColor];
        titleView.layer.borderColor = getColor(bgColor).CGColor;
        titleView.layer.borderWidth = 1;
        titleView.layer.cornerRadius = 5;
        titleView.layer.masksToBounds = YES;
        [_headerView addSubview:titleView];
        [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_chooseStypeNameBtn.mas_bottom).offset(5 / HEIGHT_6S_SCALE);
            make.left.equalTo(_headerView.mas_left).offset(10 / WIDTH_6S_SCALE);
            make.right.equalTo(_headerView.mas_right).offset(-10 / WIDTH_6S_SCALE);
            make.height.mas_equalTo(50 / HEIGHT_6S_SCALE);
        }];
        UILabel *titleLabel = [[UILabel alloc]init];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textColor = getColor(textColor);
        titleLabel.font = DEF_FontSize_14;
        [titleLabel sizeToFit];
        titleLabel.text = @"标题:";
        [titleView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(titleView.mas_centerY);
            make.left.equalTo(titleView.mas_left).offset(20 / WIDTH_6S_SCALE);
            make.height.mas_equalTo(20 / HEIGHT_6S_SCALE);
        }];
        UIView *vLineView = [[UIView alloc]init];
        vLineView.backgroundColor = getColor(bgColor);
        [titleView addSubview:vLineView];
        [vLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(titleLabel.mas_right).offset(20 / WIDTH_6S_SCALE);
            make.top.equalTo(titleView.mas_top).offset(10 / HEIGHT_6S_SCALE);
            make.bottom.equalTo(titleView.mas_bottom).offset(-10 / HEIGHT_6S_SCALE);
            make.width.mas_equalTo(1 / WIDTH_6S_SCALE);
        }];
        UITextField *titleField = [[UITextField alloc]init];
        titleField.backgroundColor = [UIColor clearColor];
        titleField.textAlignment = NSTextAlignmentLeft;
        titleField.returnKeyType = UIReturnKeyDone;
        titleField.delegate = self;
        titleField.textColor = getColor(textColor);
        titleField.placeholder = @"请输入标题";
        [titleView addSubview:titleField];
        [titleField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(vLineView.mas_right).offset(10 / WIDTH_6S_SCALE);
            make.right.top.bottom.equalTo(titleView);
        }];
    }
    return _headerView;
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)aTextfield {
    [aTextfield resignFirstResponder];//关闭键盘
    return YES;
}

- (UIView *)footerView
{
    if (!_footerView) {
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,50)];
    }
    return _footerView;
}

@end
